<template>

  <div>

    <data-table
      ref="pagingTable"
      :options="options"
      :list-query="listQuery"
      @add="handleAdd"
      @edit="handleUpdate"
    >

      <template #filter-content>

        <el-input
          v-model="listQuery.params.title"
          style="width: 200px"
          placeholder="搜索公告标题"
          class="filter-item"
          size="small"
        />

      </template>

      <template #data-columns>

        <el-table-column
          type="selection"
          width="55"
        />

        <el-table-column
          label="标题"
          prop="title"
        >

          <template v-slot="scope">
            <detail-link :id="scope.row.id" :title="scope.row.title" permission="sys:notice:update" @click="handleUpdate" />
          </template>

        </el-table-column>

        <el-table-column
          align="center"
          label="发布时间"
          prop="createTime"
        />

        <el-table-column
          align="center"
          label="状态"
        >

          <template v-slot="scope">
            {{ scope.row.state | stateFilter }}
          </template>
        </el-table-column>

      </template>
    </data-table>

    <el-dialog
      :close-on-click-modal="false"
      :modal-append-to-body="false"
      :visible.sync="dialogVisible"
      title="公告管理"
      width="50%"
    >

      <el-form ref="postForm" :model="formData" :rules="rules" label-position="top">

        <el-form-item label="标题" prop="title">
          <el-input v-model="formData.title" size="small" />
        </el-form-item>

        <el-form-item label="内容" prop="content">
          <Tinymce ref="editor" v-model="formData.content" :height="300" />
        </el-form-item>

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </div>

    </el-dialog>

  </div>

</template>

<script>
import DataTable from '@/components/DataTable'
import { saveData, fetchDetail } from '@/api/sys/notice/notice'
import Tinymce from '@/components/Tinymce'
import DetailLink from '@/components/DetailLink'

export default {
  components: { DetailLink, Tinymce, DataTable },
  filters: {

    // 订单状态
    userState(value) {
      const map = {
        '0': '正常',
        '1': '禁用'
      }
      return map[value]
    }
  },
  data() {
    return {

      treeData: [],
      defaultProps: {
        value: 'id',
        label: 'deptName',
        children: 'children'
      },
      dialogVisible: false,

      rules: {

        title: [
          { required: true, message: '公告标题不能为空！' }
        ],
        content: [
          { required: true, message: '公告内容不能为空！' }
        ]
      },

      listQuery: {
        current: 1,
        size: 10,
        params: {
        }
      },

      formData: {
        title: '',
        content: ''
      },

      options: {
        add: {
          enable: true,
          permission: 'sys:notice:add'
        },
        edit: {
          enable: true,
          permission: 'sys:notice:update'
        },
        delete: {
          enable: true,
          permission: 'sys:notice:delete',
          url: '/api/sys/notice/delete'
        },
        // 列表请求URL
        listUrl: '/api/sys/notice/paging',
        // 批量操作列表
        multiActions: [
          {
            value: 'enable',
            label: '启用',
            permission: 'sys:notice:update',
            url: '/api/sys/notice/state'
          }, {
            value: 'disable',
            label: '禁用',
            permission: 'sys:notice:update',
            url: '/api/sys/notice/state'
          }
        ]
      }
    }
  },

  created() {

  },

  methods: {

    handleUploadSuccess(response) {
      // 上传图片赋值
      this.formData.avatar = response.data.url
    },

    handleAdd() {
      this.formData = {}
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.editor.setContent('')
      })
    },

    handleUpdate(id) {
      this.dialogVisible = true
      fetchDetail(id).then(res => {
        this.formData = res.data
        this.$refs.editor.setContent(this.formData.content)
      })
    },

    handleSave() {
      this.$refs.postForm.validate((valid) => {
        if (!valid) {
          return
        }
        saveData(this.formData).then(() => {
          this.$message({
            type: 'success',
            message: '公告保存成功!'
          })
          this.dialogVisible = false
          this.$refs.pagingTable.getList()
        })
      })
    }
  }
}
</script>

<style scoped>

  .el-dialog__body{
    padding: 0px 20px;
  }

  ::v-deep
  .el-dialog__wrapper{
    z-index: 0;
  }

</style>
